<?php
// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

class BmUserEarningstAction extends CommonAction
{
    public function index()
    {
		$id=intval($_REQUEST['id']);
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);

     	$bm_qrcode_id=trim($_REQUEST['bm_qrcode_id']);
     	//时间
        $time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
		
        $where = " bpg.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . "";
     	$where .= " and u.is_effect = 1  and bp.member_id = 0 and bp.`status` = 1 and bp.type = 2";
		
        if($id > 0){
     		$where .= " and u.id = ".$id." ";
     	}
		if($login_name){
     		$where .=" and bp.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and bp.login_name like '%" . $login_name1 . "%' ";
     	}
     	if($bm_qrcode_id != ""){
     		$where .= " and  u.bm_qrcode_id = ".$bm_qrcode_id." ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		
        $sql_count = "SELECT 
						count(*) as list_count 
					FROM  
						" . $pre . "bm_promoter_game_log as bpg 
					LEFT JOIN ". $pre . "user as u on bpg.user_id = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.bm_pid = bp.user_id  
					WHERE 
						$where ";
					
		$list_count_row=$GLOBALS['db']->getRow($sql_count);
		
		$p = $_REQUEST['p'];
		if ($p == '') {
			$p = 1;
		}
		$p         = $p > 0 ? $p : 1;
		$page_size = 10;
		$limit     = (($p - 1) * $page_size) . "," . $page_size;

		$count     = intval($list_count_row['list_count']);

        if($count > 0){

			$page      = new Page($count, $page_size);
			$page_show = $page->show();
	
			$sql = "SELECT 
						u.id,
						u.nick_name,
						bpg.user_gain,
						bpg.platform_gain,
						bpg.promoter_gain,
						bpg.gain,
						u.bm_pid,
						u.bm_qrcode_id,
						u.bm_promoter_id,
						bp.login_name as bm_pid_login_name 
					FROM   
						". $pre . "bm_promoter_game_log as bpg 
					LEFT JOIN ". $pre . "user as u on bpg.user_id = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.bm_pid = bp.user_id  
					WHERE 
						".$where." 
					order by ".$order." ".$sort." limit ".$limit;

			$list = $GLOBALS['db']->getAll($sql);
			foreach($list as $k=>$v)
            {
            	//代理商编号
            	$list[$k]['bm_promoter_login_name'] = $GLOBALS['db']->getOne("select login_name from ". $pre . "bm_promoter where id=".$v['bm_promoter_id']);
            }
		}
		
		//会员手续费,平台留存手续费,鱼商留存手续费,鱼商客损
		$total = $GLOBALS['db']->getRow( 
			"SELECT 
				sum(user_gain) as sum_user_gain,
				sum(bpg.platform_gain ) as total_platform_gain,
				sum(bpg.promoter_gain) as total_promoter_gain,sum(bpg.gain) as total_gain 
			FROM   
				". $pre . "bm_promoter_game_log as bpg 
			LEFT JOIN ". $pre . "user as u on bpg.user_id = u.id 
			LEFT JOIN ". $pre . "bm_promoter as bp on u.bm_pid = bp.user_id  
			WHERE 
				$where ");
		
		
		$sortImg = $sort; //排序图标
		$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
		$sort = $sort == 'desc' ? 1 : 0; //排序方式
		//模板赋值显示

		$this->assign ( 'list', $redis['list'] );
		$this->assign ( 'sort', $sort );
		$this->assign ( 'order', $order );
		$this->assign ( 'sortImg', $sortImg );
		$this->assign ( 'sortType', $sortAlt );
		
		
        $this->assign('list', $list);
        $this->assign('page', $page_show);
		$this->assign('total', $total);
		$this->assign('begin_time', to_date($time['begin_time'], 'Y-m-d'));
		$this->assign('end_time', to_date($time['end_time'], 'Y-m-d'));
		$this->assign('id', $id);
		$this->assign('bm_qrcode_id', $bm_qrcode_id);
		$this->assign('login_name', $login_name);
		
		$bm_config = load_auto_cache("bm_config");
		$this->assign('bm_one', $bm_config['bm_one']);
		$this->assign('bm_two', $bm_config['bm_two']);
		
        $this->display();
    }
	//导出电子表
    public function export_csv($page = 1)
    {

        $pagesize = 10;
        set_time_limit(0);
        $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
		
		$date = $_REQUEST;
		
		/*********************/
		
		/*------------------------*/
		$id=intval($_REQUEST['id']);
        $login_name=strim($_REQUEST['login_name']);
        $login_name1=strim($_REQUEST['login_name1']);

     	$bm_qrcode_id=trim($_REQUEST['bm_qrcode_id']);
     	//时间
        $time = $this->check_date();
		if($time['status']==0)
		{
			$this->error($time['error']);
		}
		
        $where = " bpg.create_time between " . $time['begin_time'] . " and " . $time['end_time'] . "";
     	$where .= " and u.is_effect = 1  and bp.member_id = 0 ";
		
        if($id > 0){
     		$where .= " and u.id = ".$id." ";
     	}
		if($login_name){
     		$where .=" and bp.login_name = '".$login_name."' ";
     	}elseif ($login_name1){
     		$where .= " and bp.login_name like '%" . $login_name1 . "%' ";
     	}
     	if($bm_qrcode_id != ""){
     		$where .= " and  u.bm_qrcode_id = ".$bm_qrcode_id." ";
     	}
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = strim($_REQUEST ['_order']);
		} else {
			$order = "u.id";
		}
		
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		$pre = DB_PREFIX;
		
        $sql_count = "SELECT 
						count(*) as list_count 
					FROM  
						" . $pre . "bm_promoter_game_log as bpg 
					LEFT JOIN ". $pre . "user as u on bpg.user_id = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.bm_pid = bp.user_id  
					WHERE 
						$where ";
					
		$list_count_row=$GLOBALS['db']->getRow($sql_count);


		$count     = intval($list_count_row['list_count']);

        if($count > 0){

	
			$sql = "SELECT 
						u.id,
						u.nick_name,
						bpg.user_gain,
						bpg.platform_gain,
						bpg.promoter_gain,
						bpg.gain,
						u.bm_pid,
						u.bm_qrcode_id,
						u.bm_promoter_id,
						bp.login_name as bm_pid_login_name 
					FROM   
						". $pre . "bm_promoter_game_log as bpg 
					LEFT JOIN ". $pre . "user as u on bpg.user_id = u.id 
					LEFT JOIN ". $pre . "bm_promoter as bp on u.bm_pid = bp.user_id  
					WHERE 
						".$where." 
					order by ".$order." ".$sort." limit ".$limit;

			$list = $GLOBALS['db']->getAll($sql);
			foreach($list as $k=>$v)
            {
            	//代理商编号
            	$list[$k]['bm_promoter_login_name'] = $GLOBALS['db']->getOne("select login_name from ". $pre . "bm_promoter where id=".$v['bm_promoter_id']);
            }
		}
		else
		{
			if($page==1)
				$this->error(L("NO_RESULT"));
		}

		/*------------------------*/
		/*********************/
		$voList = $list;
		
        if($voList)
        {
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? l("ASC_SORT") : l("DESC_SORT"); //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
	
			
			$bm_config = load_auto_cache("bm_config");
			
            register_shutdown_function(array(&$this, 'export_csv'), $page+1);

            $refund_value = array( 'id'=>'""', 'nick_name'=>'""', 'user_gain'=>'""','platform_gain'=>'""','promoter_gain'=>'""','gain'=>'""','bm_pid_login_name'=>'""','bm_promoter_login_name'=>'""','bm_qrcode_id'=>'""');
            if($page == 1)
            {
                $content = iconv("utf-8","gbk","会员ID,会员名称,会员游戏盈亏,平台留存手续费,".$bm_config['bm_one']."留存手续费,".$bm_config['bm_two']."客损,所属".$bm_config['bm_two']."编号,所属代理商编号,所属业务员ID");
                $content = $content . "\n";
            }


            foreach($voList as $k=>$v)
            {
				$refund_value['id'] = '"' . iconv('utf-8','gbk',$v['id']) . '"'."\t";
				$refund_value['nick_name'] = '"' . iconv('utf-8','gbk',$v['nick_name']) . '"'."\t";
				$refund_value['user_gain'] = '"' . iconv('utf-8','gbk',$v['user_gain']) . '"'."\t";
				$refund_value['platform_gain'] = '"' . iconv('utf-8','gbk',$v['platform_gain']) . '"'."\t";
				$refund_value['promoter_gain'] = '"' . iconv('utf-8','gbk',$v['promoter_gain']) . '"'."\t";
				$refund_value['gain'] = '"' . iconv('utf-8','gbk',$v['gain']) . '"'."\t";
				$refund_value['bm_pid_login_name'] = '"' . iconv('utf-8','gbk',$v['bm_pid_login_name']) . '"'."\t";
				$refund_value['bm_promoter_login_name'] = '"' . iconv('utf-8','gbk',$v['bm_promoter_login_name']) . '"'."\t";
				$refund_value['bm_qrcode_id'] = '"' . iconv('utf-8','gbk',$v['bm_qrcode_id']) . '"'."\t";
				
                $content .= implode(",", $refund_value) . "\n";
            }

            //
            header("Content-Disposition: attachment; filename=refund_list.csv");

            echo $content ;
        }
        else
        {
            if($page==1)
                $this->error(L("NO_RESULT"));
        }

    }
}
